<?php
namespace EasySwoole\EasySwoole;


use App\Template;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;
use EasySwoole\Template\Render;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        // 在此创建好的对象，只要没有被作用域销毁，就会在程序启动后就会一直存在，直到整个程序结束运行才会销毁，程序全局对象所占用的内存是worker进程间共享的，不会额外占用内存
        date_default_timezone_set('Asia/Shanghai');
        //从配置文件dev.php 或 produce.php 中获取 MYSQL配置, 注册MYSQL 链接池
        $config  = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
        $config->setMaxObjectNum(20);   //配置连接池最大数量
        DbManager::getInstance()->addConnection(new Connection($config));

    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.

        //配置热更新代码
        $hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions();
        $hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
        $hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);

        //根据应用的名称获取应用实例
        $server = ServerManager::getInstance()->getSwooleServer();
        //附加到当前的服务
        $hotReload->attachToServer($server);

        //注册模板驱动
        Render::getInstance()->getConfig()->setRender(new Template());
        Render::getInstance()->attachServer(ServerManager::getInstance()->getSwooleServer());

    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}